import axios from 'axios'
import qs from 'qs'

export function http(url,options,isHost = true){
  // console.log(process.env.NODE_ENV == 'production')
  // console.log('process.env.BASEURL',globalVar.httpType+process.env.BASEURL)
  const Host = isHost ? process.env.BASEURL : ''
  const defaultOptions = {url:Host+url,timeout:5000}
  const newOptions = { ...defaultOptions, ...options}

  // if(isAddToken) newOptions.headers = {'ACCESS-TOKEN':storage.get('TOKEN'),...newOptions.headers}

  if(newOptions.method !== 'get'){
    newOptions.data = qs.stringify(newOptions.data)
    newOptions.headers = {'Content-Type':'application/x-www-form-urlencoded',...newOptions.headers}
  }

  return axios(newOptions)
    // .then(checkStatus)
    .then(response =>{
      return response.data
    })
    .catch(function (error) {
      console.warn(error.message)
    })
    // .then(checkCode)
}

export function post(url, data) {
  return http(url,{method:'post',data})
}

export function get(url, params = {}) {
  return http(url,{method:'get',params:params})
}



